﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BoAddonBiz_FreightForwarder.Air.AirPortWork.Warehouse
{
    class B1Form_Delivery : SAPBoAddon.B1Form
    {
        IList<Cargo> cargoList = null;
        #region override
        protected override bool BeforeSaveCheckFormData()
        {
            return true;
        }

        protected override void FormEditModeChange(SAPBoAddon.frmItemEditMode frmEditMode)
        {

        }

        protected override void FormInfoSetting(SAPBoAddon.B1FormSetting FormSetting)
        {
            FormSetting.FormType = B1FormTypes.ado_AVA_FF_BJSDelivery;
            FormSetting.FormFileName = "AVA_DeliveryForm.srf";
        }

        protected override void InitializeForm(SAPBoAddon.B1FormInitializePar InitPar)
        {
            cargoList = InitPar.parObject as IList<Cargo>;
        }
        #endregion

        #region constructor
        public B1Form_Delivery()
        {
            this.frm_ItemEvent += new SAPBoAddon.IB1FormEvents.B1ItemEventEventHandler(B1Form_Delivery_frm_ItemEvent);
            this.frm_ChooseFormListEvent += new SAPBoAddon.IB1FormEvents.B1ChooseFormListEventEventHandler(B1Form_Delivery_frm_ChooseFormListEvent);
        }

        void B1Form_Delivery_frm_ChooseFormListEvent(SAPBoAddon.B1AddonBase.B1ChooseFormList pVal, ref bool BubbleEvent)
        {
            if (pVal.Before_Action)
            {
                SAPbouiCOM.ChooseFromList cfl = this.CurrentForm.ChooseFromLists.Item(pVal.ChooseFromListUID);
                SAPbouiCOM.Conditions cons;
                SAPbouiCOM.Condition con;
                switch (pVal.ChooseFromListUID)
                {
                    case "CardName":
                        cons = new SAPbouiCOM.Conditions();
                        //con = cons.Add();
                        //con.Alias = "QryGroup13";
                        //con.CondVal = "Y";
                        //con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        //con.Relationship = BoConditionRelationship.cr_AND;
                        //con = cons.Add();
                        //con.Alias = "CardType";
                        //con.CondVal = "C";
                        //con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        //con.Relationship = BoConditionRelationship.cr_AND;
                        con = cons.Add();
                        con.Alias = "validFor";
                        con.CondVal = "Y";
                        con.Operation = SAPbouiCOM.BoConditionOperation.co_EQUAL;
                        cfl.SetConditions(cons);
                        break;
                }
            }
            else
            {
                if (pVal.Action_Success)
                {
                    if (pVal.ChooseFromListUID == "CardName")//客户
                    {
                        this.CurrentForm.DataSources.UserDataSources.Item("CardCode").Value = pVal.SelectedObjects.GetValue("CardCode", 0).ToString();
                        this.CurrentForm.DataSources.UserDataSources.Item("CardName").Value = pVal.SelectedObjects.GetValue("CardName", 0).ToString();
                        dynamic Cntct = Utils.GetDefaultContractPerson(this.SBOCompany, pVal.SelectedObjects.GetValue("CardCode", 0).ToString());
                        if (Cntct != null)
                        {
                            this.CurrentForm.DataSources.UserDataSources.Item("Person").Value = Cntct.FullName;
                        }
                    }

                }
            }
        }

        void B1Form_Delivery_frm_ItemEvent(SAPBoAddon.B1AddonBase.B1ItemEvent pVal, ref bool BubbleEvent)
        {
            if (pVal.BeforeAction == false)
            {
                if (pVal.ItemUID == "3" && pVal.EventType == SAPbouiCOM.BoEventTypes.et_ITEM_PRESSED)
                {
                    if (cargoList.Count > 0)
                    {
                        string CardCode = CurrentForm.DataSources.UserDataSources.Item("CardCode").Value;
                        string CardName = CurrentForm.DataSources.UserDataSources.Item("CardName").Value;
                        string Person = CurrentForm.DataSources.UserDataSources.Item("Person").Value;
                        SAPbobsCOM.Recordset rs = SBOCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.BoRecordset);
                        if (SBOCompany.InTransaction == false) SBOCompany.StartTransaction();
                        string SQL = @"
                                    UPDATE [@AVA_FF_STG1] SET U_OutOrIn = 'Out',U_DeliveryCode = '{2}',U_DeliveryName = '{3}',U_DeliveryPerson = '{4}' Where DocEntry = '{0}' And LineId = '{1}'
                                    IF Not Exists(
	                                    SELECT *
	                                    FROM [@AVA_FF_OSTG] T0
		                                    INNER JOIN [@AVA_FF_STG1] T1 ON T0.DocEntry = T1.DocEntry
	                                    Where T0.Canceled = 'N' And T0.U_Status <> 'D'
		                                    And T1.U_OutOrIn = 'In'
		                                    And T0.DocEntry = '{0}'
	                                    )
                                    Begin
	                                    UPDATE [@AVA_FF_OSTG] SET U_Status = 'C' Where DocEntry = '{0}'
                                    End
                                    ";
                        foreach (Cargo cargo in cargoList)
                        {
                            rs.DoQuery(string.Format(SQL, cargo.DocEntry, cargo.LineId, CardCode, CardName, Person));
                        }
                        if (SBOCompany.InTransaction) SBOCompany.EndTransaction(SAPbobsCOM.BoWfTransOpt.wf_Commit);
                        CurrentForm.Close();
                        SBOApp.MessageBox("操作成功！");
                    }
                }
            }
        }
        #endregion
    }
}
